<?php

namespace app\mp\admin;

use app\common\service\UserDefinedGet as UserDefinedGetService;
use app\system\admin\Admin;
use think\Db;

/**
 * 自定义导出
 * @author zhuguosen 905502314@qq.com
 * @date 2022/6/7
 * @package app\mp\admin
 */
class UserDefinedGet extends Admin
{
    /**
     * @var \app\common\service\UserDefinedGet
     */
    protected $userDefinedGetService;

    public function initialize()
    {
        parent::initialize();
        $this->userDefinedGetService = new UserDefinedGetService();
    }

    /**
     * 物料进度
     * @author zhuguosen 905502314@qq.com
     * @date 2022/4/3
     */
    public function index()
    {
        $stat = $this->request->param('a/s', '');
        if ($this->request->isAjax()) {
            $page = $this->request->param('page/d', 1);
            $limit = $this->request->param('limit/d', 20);
            $where = $this->request->param();
            $list = $this->userDefinedGetService->allList('list', $page, $limit, $where, true);
            return json($list);
        }
        $factoryKV = getFactoryKV(false);
        $stallsKV = getStallsKV();
        $assign = [
            'action' => $stat,
            'factoryKV' => [0 => '请选择'] + $factoryKV,
            'stallsKV' => [0 => '请选择'] + $stallsKV,
            'orderFrom' => [0 => '请选择'] + config('mimo.order_from'),
            'purchasingUserKV' => [0 => '请选择'] + getTagUserKV('tag_purchasing_user'),
        ];
        return $this->assign($assign)->fetch();
    }

    public function labelSet()
    {
        $param = $this->request->param();
        if ($this->request->isAjax() || $this->request->param('export/d', 0)) {
            $insertData = [
                'name' => date('Y-m-d H:i:s'),
                'where' => base64_decode($param['whereData']),
                'label' => json_encode(['order' => $param['order'] ?? [], 'material' => $param['material'] ?? [],]),
                'status' => 0,
                'file_url' => '',
                'admin_id' => ADMIN_ID,
                'create_time' => time(),
            ];
            db('user_defined')->insert($insertData);
            $result = [
                'code' => 1,
                'msg' => '正写入数据',
            ];
            return json($result);
        }
        $this->assign('whereData', base64_encode(json_encode($param)));
        $this->assign('order_kv', $this->userDefinedGetService->order_kv);
        $this->assign('material_kv', $this->userDefinedGetService->material_kv);
        return $this->fetch();
    }

    public function run()
    {
        set_time_limit(0);
        ini_set('memory_limit', '512M');
        error_reporting(E_ALL & ~E_NOTICE);
        $this->userDefinedGetService->head_flag = 0;
        $res = $this->userDefinedGetService->set_excel_data();
        return json($res);
    }

    public function getFile()
    {
        if ($this->request->isAjax() || $this->request->param('export/d', 0)) {
            $list = db('user_defined ud')
                ->field('ud.*,su.username')
                ->join('system_user su', "ud.admin_id=su.id", 'left')
                ->select();
            $count = db('user_defined')->count();
            foreach ($list as $k => $v) {
                $list[$k]['download_url'] = $this->request->domain() . '/' . $v['upload_path'];
                $list[$k]['create_time'] = date('Y-m-d H:i:s', $v['create_time']);
                $list[$k]['status'] = $v['status'] == 1 ? '已完' : '未完';
                $list[$k]['finish_num'] = '0.00%';
                if ($v['status_ext'] != '') {
                    $temp = json_decode($v['status_ext'], true);
                    //{"count":2,"page":1,"limit":5}
                    $a = ($temp['page'] * $temp['limit']) / $temp['count'];
                    $a >= 1 && $a = 100;
                    $a < 1 && $a = $a * 100;
                    $a = sprintf("%.2f", $a);
                    $list[$k]['finish_num'] = "{$a}%";
                }
            }
            $result = [
                'code' => 0,
                'msg' => '',
                'data' => $list,
                'count' => $count,
            ];
            return json($result);
        }
        return $this->fetch();
    }

    public function delFile()
    {
        $id = $this->request->param('id/d');
        $info = db('user_defined')->where([
            ['id', '=', $id],
        ])->find();
        //删除文件
        $file_url = env('root_path') . 'public/' . $info['upload_path'];
        if (file_exists($file_url) && is_file($file_url)) {
            unlink($file_url);
        }
        //删除记录
        db('user_defined')->where([
            ['id', '=', $id],
        ])->delete();
        return $this->success('删除成功', '');
    }
}